package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.DTO.AddPrizePoolBO;
import com.ruoyi.common.core.domain.DTO.CashOutBO;
import com.ruoyi.common.core.domain.DTO.UpdatePrizePoolBO;
import com.ruoyi.common.core.domain.DTO.UpdateStatusBO;
import com.ruoyi.common.core.domain.entity.PrizePool;
import com.ruoyi.common.core.domain.entity.UserPrizeRecord;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.PrizeService;
import com.ruoyi.framework.web.BO.SaveUserPrizeRecordBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/prize")
public class PrizeController extends BaseController {
    @Autowired
    private PrizeService prizeService;

    /**
     * 添加奖池
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated AddPrizePoolBO addPrizePoolBO) {
        prizeService.add(addPrizePoolBO);
        return success();
    }

    /**
     * 修改奖池
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody UpdatePrizePoolBO updatePrizePoolBO) {
        prizeService.update(updatePrizePoolBO);
        return success();
    }

    /**
     * 启用/禁用奖池
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody @Validated UpdateStatusBO updateStatusBO) {
        prizeService.updateStatus(updateStatusBO);
        return success();
    }

    /**
     * 根据等级查询奖品池
     * @return
     */
    @GetMapping("/list/{level}")
    public TableDataInfo list(@PathVariable Integer level) {
        List<PrizePool> list = prizeService.selectPrizePoolList(level);
        return getDataTable(list);
    }

    /**
     * 查询所有奖品池
     */
    @GetMapping("/list")
    public TableDataInfo listAll() {
        List<PrizePool> list = prizeService.selectPrizePoolList(null);
        return getDataTable(list);
    }

    /**
     * 保存用户中奖记录
     */
    @PostMapping("/saveUserPrizeRecord")
    public AjaxResult saveUserPrizeRecord(@RequestBody SaveUserPrizeRecordBO saveUserPrizeRecordBO) {
        return success(prizeService.saveUserPrizeRecord(saveUserPrizeRecordBO));
    }

    /**
     * 查询用户中奖记录
     * @param userId
     * @return
     */
    @GetMapping("/userPrizeRecordList/{userId}")
    public TableDataInfo userPrizeRecordList(@PathVariable Long userId) {
        List<UserPrizeRecord> list = prizeService.selectUserPrizeRecordList(userId);
        return getDataTable(list);
    }

    /**
     * 使用奖品
     */
    @PostMapping("/cash/out")
    public AjaxResult cashOut(@RequestBody @Validated CashOutBO cashOutBO) {
        prizeService.cashOut(cashOutBO.getId());
        return success();
    }
}
